#!/usr/bin/env python

# show_rosette.py

from __future__ import division

import math
from optparse import OptionParser

from ot_simulator import graph
from ot_simulator.template import template_funs

Max_dimensions = 400

def to_cartesian(r, theta):
    theta = math.radians(theta)
    return r * math.cos(theta), r * math.sin(theta)

def run(rosette_csv, repeats=4, radius=1.0, outputfile=None):
    template = template_funs.repeating_template(
                 (x * 360/repeats, y)
                 for x, y in template_funs.file_multi_num(rosette_csv))

    g = graph.graph(Max_dimensions)
    #g.line((-semi_minor, -semi_major), (-semi_minor, semi_major),
    #       color="rgb(0, 255, 0)")
    g.plot(to_cartesian(template(angle_deg) + radius, angle_deg)
           for angle_deg in template_funs.frange(1, 0, 360))
    if not outputfile:
        g.show()
    else:
        g.write(outputfile)


if __name__ == "__main__":
    import sys
    parser = OptionParser(usage="%prog [options] rosette.csv",
                          version= '%prog 0.1.0')

    parser.add_option('-r', "--repeats", type="int", default=4,
                      help="number of repetitions (4)",
                     )

    parser.add_option("--radius", type="float", default=1.0,
                     )

    parser.add_option('-o', "--output", type="string", default=None,
                     )

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("incorrect number of arguments")

    run(args[0], options.repeats, options.radius, options.output)
